package org.mzy.org.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.mzy.basic.anno.PreAuthorize;
import org.mzy.basic.util.AjaxResult;
import org.mzy.basic.util.LoginContext;
import org.mzy.basic.util.PageList;
import org.mzy.org.domain.Employee;
import org.mzy.org.query.EmployeeQuery;
import org.mzy.org.service.IEmployeeService;
import org.mzy.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Package: org.mzy.org.controller
 * @ClassName: EmployeeController
 * @Date:2022/10/9 17:58
 * @Description:
 */

@RestController
@RequestMapping("/emp")
@Api(value = "员工的API", description = "员工相关的CRUD功能")
public class EmployeeController {
    // 调service干活
    @Autowired
    private IEmployeeService employeeService;

    // 查询全部
    @GetMapping
    @ApiOperation(value = "查询所有作者的信息")
    @PreAuthorize(name = "查询所有员工", sn = "emp:findAll")
    public List<Employee> findAll() {
        return employeeService.findAll();
    }

    // 根据ID删除单条
    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除一条数据")
    @PreAuthorize(name = "删除员工", sn = "emp:delete")
    public AjaxResult delete(@PathVariable("id") Long id) {
        employeeService.delete(id);
        return AjaxResult.me().success();
    }

    // 批量删除
    @PatchMapping
    @ApiOperation("批量删除")
    @PreAuthorize(name = "批量删除员工", sn = "emp:batchDel")
    public AjaxResult batchDel(@RequestBody List<Long> ids) {
        employeeService.batchDel(ids);
        return AjaxResult.me().success();
    }

    // 新增或修改
    @PutMapping
    @ApiOperation(value = "新增或修改")
    @PreAuthorize(name = "新增/修改员工", sn = "emp:addOrUpdate")
    public AjaxResult addOrUpdate(@RequestBody Employee employee) {
        if (employee.getId() == null) {
            employeeService.add(employee);
        } else {
            System.out.println("===========");
            employeeService.update(employee);
        }
        return AjaxResult.me().success();
    }

    // 根据ID查询单条
    @GetMapping("/{id}")
    @ApiOperation(value = "根据ID查询一条数据")
    @PreAuthorize(name = "通过ID查询员工", sn = "emp:findById")
    public Employee findById(@ApiParam(value = "ID属性", required = true) @PathVariable("id") Long id) {
        return employeeService.findById(id);
    }

    // 分页查询
    @PostMapping
    @ApiOperation("分页查询")
    @PreAuthorize(name = "分页查询员工", sn = "emp:queryByPage")
    public PageList<Employee> queryByPage(@RequestBody EmployeeQuery employeeQuery) {
        return employeeService.queryByPage(employeeQuery);
    }

    // 根据店铺ID查询单条
    @GetMapping("/selectByShopId")
    @ApiOperation(value = "根据ID查询一条数据")
    public List<Employee> selectByShopId(HttpServletRequest request) {
        // 通过前端请求获取登录信息
        Logininfo loginUser = LoginContext.getLoginUser(request);
        // 通过登录信息的Id获取员工信息
        Employee employee = employeeService.selectByLogininfoId(loginUser.getId());
        // 通过员工的店铺id获取所有员工
        return employeeService.findByShopId(employee.getShopId());
    }
}
